package com.bosssoft.promotions.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.base.entity.Result;
import com.bosssoft.base.enums.ResultCodeEnum;
import com.bosssoft.promotions.domain.dto.PromotionsDTO;
import com.bosssoft.promotions.domain.dto.PromotionsQueryDTO;
import com.bosssoft.promotions.domain.po.Promotions;
import com.bosssoft.promotions.domain.vo.PromotionsVO;
import com.bosssoft.promotions.service.PromotionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/promotions")
@Api(value = "促销活动类")
public class PromotionsController {

    @Autowired
    private PromotionsService promotionsService;

    /*
     * 用例图完
     */
    @ApiOperation(value = "添加促销活动")
    @PostMapping("add")
    public Result<PromotionsDTO> addPromotion(@RequestBody PromotionsDTO promotionsDTO) {
        if (promotionsService.addPromotion(promotionsDTO)) {
            return Result.build(ResultCodeEnum.SUCCESS.getCode(), "添加成功", null);
        }
        return Result.build(ResultCodeEnum.ERROR.getCode(), "添加失败", null);

    }

    /*
     * 用例图完
     */
    @ApiOperation(value = "更新促销活动详情")
    @PostMapping("update")
    public Result updatePromotion(@RequestBody PromotionsDTO promotionsDTO) {
        if (promotionsService.updatePromotion(promotionsDTO)) {
            return Result.build(ResultCodeEnum.SUCCESS.getCode(), "更新成功", null);
        }
        return Result.build(ResultCodeEnum.ERROR.getCode(), "更新失败", null);
    }

    /*
     * 用例图完
     */
    @ApiOperation(value = "删除促销活动")
    @PostMapping("delete")
    public Result deletePromotionById(@RequestBody PromotionsQueryDTO promotionsQueryDTO) {
        if (promotionsService.deletePromotionById(promotionsQueryDTO)) {
            return Result.build(ResultCodeEnum.SUCCESS.getCode(), "删除成功", null);
        }
        return Result.build(ResultCodeEnum.ERROR.getCode(), "删除失败", null);
    }

    /*
     * 用例图完
     */
    @ApiOperation(value = "批量删除促销活动")
    @PostMapping("batchdelete")
    public Result batchDeletePromotionById(@RequestBody List<PromotionsQueryDTO> idList) {
        if (promotionsService.batchDeletePromotionById(idList)) {
            return Result.build(ResultCodeEnum.SUCCESS.getCode(), "批量删除成功", null);
        }
        return Result.build(ResultCodeEnum.ERROR.getCode(), "批量删除失败", null);
    }

    /*
     * POSTMAN 通过
     */
    @ApiOperation(value = "查询促销活动")
    @GetMapping("query")
    public Result queryPromotions(PromotionsQueryDTO promotionsQueryDTO){
        Promotions promotions = promotionsService.queryPromotions(promotionsQueryDTO);
        PromotionsVO promotionsVO = new PromotionsVO();
        promotionsVO.setBeginTime(promotions.getBeginTime());
        promotionsVO.setDescription(promotions.getDescription());
        promotionsVO.setEndTime(promotions.getEndTime());
        promotionsVO.setPromotion(promotions.getPromotion());
        promotionsVO.setStatus(promotions.getStatus());
        return Result.build(ResultCodeEnum.SUCCESS.getCode(), "查询成功", promotionsVO);
    }

    /*
     * 查询全部活动（分页）
     */
    @ApiOperation(value = "查询全部活动")
    @GetMapping("queryAllPagePromotions")
    public Result queryAllPagePromotions(int page,int size){
        Page<Promotions> promotionsPage = promotionsService.QueryAllPagePromotions(page, size);
        return Result.build(ResultCodeEnum.SUCCESS.getCode(), "查询成功", promotionsPage);
    }


}

